fn main() {
    // 创建一个空的 vector
    let v: Vec<i32> = Vec::new();
    // 使用宏vec!创建一个 vector，并使用初始值填充它
    {
        let mut v2 = vec![1, 2, 3];
        //更新
        v2.push(5);
        v2.push(6);

        //遍历
        for i in &v2 {
            println!("{}", i);
            
        }
    }

    //遍历 可变元素
    {
        let mut v3 = vec![1, 2, 3];
        for i in &mut v3 {
            *i += 50;
        }
    }

    // 作用域
    {
        let v3 = vec![1, 2, 3, 4, 5];

        let third: &i32 = &v3[2];

    } // 离开作用域，v就会被销毁
    let v4 = vec![1, 2, 3, 4, 5];
    // 访问元素
    let third: &i32 = &v4[2];

    // 使用get方法 ，会得到一个Option<&i32>
    match v.get(2) {
        Some(third) => println!("The third element is {}", third),
        None => println!("There is no third element."),
    }

    #[derive(Debug)]
    enum SpreadsheetCell {
        Int(i32),
        Float(f64),
        Text(String),
    }
    let row = vec![
        SpreadsheetCell::Int(3),
        SpreadsheetCell::Text(String::from("blue")),
        SpreadsheetCell::Float(10.12),
    ];
    println!("{:?}", row);

    let mut s = String::new();

    let data = "initial contents";

    let s = data.to_string();

    println!("{}", s);

    let s = "initial contents".to_string();

    println!("{}", s);

    let s = String::from("initial contents");

    println!("{}", s);

    let hello = String::from("السلام عليكم");

    println!("{}", hello);

    let hello = "Здравствуйте";

    println!("{}", hello);

    let mut s = String::from("foo");
    s.push_str("bar");
    println!("{}", s);
    s.push('!');
    println!("{}", s);

    let mut s1 = String::from("foo");
    let s2 = "bar";
    s1 += s2;
    println!("{}", s1);
    let s1 = String::from("tic");
    let s2 = String::from("tac");
    let s3 = s1 + "-" + &s2;
    println!("{}", s3);
    let s1 = String::from("tic");
    let s2 = String::from("tac");
    let s3 = format!("{}-{}", s1, s2);
    println!("{}", s3);

    // let s = String::from("hello");
    // let h = &s[0]; // error
    // println!("{}", h);
    let len = s.len();
    println!("{}", len);
    let len = String::from("Зд").len();
    println!("{}", len);

    let s = String::from("hello world");
    let hello = &s[0..5];
    let world = &s[6..11];
    println!("{} {}", hello, world);
    let s = String::from("hello");
    let slice = &s[0..2];
    let slice = &s[..2];
    println!("{}", slice);
    let len = s.len();
    let slice = &s[3..len];
    let slice = &s[3..];
    println!("{}", slice);
    //对于特殊字符

    // let s = String::from("你好，世界");
    // let s1 = &s[6..10];// 错误
    // println!("{}", s1);

    //遍历
    for c in "Зд".chars() {
        println!("{}", c);
    }
    for b in "你好，世界".bytes() {
        println!("{}", b);
    }

    use std::collections::HashMap;

    let mut scores = HashMap::new();

    scores.insert(String::from("Blue"), 10);
    scores.insert(String::from("Yellow"), 50);
    println!("{:?}", scores);

    let teams = vec![String::from("Blue"), String::from("Yellow")];
    let initial_scores = vec![10, 50];

    let scores: HashMap<_, _> = teams.iter().zip(initial_scores.iter()).collect();
    println!("{:?}", scores);

    let field_name = String::from("Favorite color");
    let field_value = String::from("Blue");
    
    let mut map = HashMap::new();
    
    map.insert(field_name, field_value);
    println!("{:?}", map);
    // println!("{:?}", field_name);// 不可访问
    let mut scores = HashMap::new();

    scores.insert(String::from("Blue"), 10);
    scores.insert(String::from("Yellow"), 50);
    
    let team_name = String::from("Blue");
    // 获取 team_name 对应的值,
    let score = scores.get(&team_name);
    println!("{:?}", score);
    // 使用 Option
    match score {
        Some(score) => println!("match {}", score),
        None => println!("match None"),
    }

    let score = scores.get(&team_name).copied().unwrap_or(0);
    println!("unwrap_or {}", score);
    
    // 遍历哈希映射
    for (key, value) in &scores {
        println!("{}: {}", key, value);
    }

    let mut scores = HashMap::new();

    scores.insert(String::from("Blue"), 10);
    // 覆盖
    scores.insert(String::from("Blue"), 25);

    println!("{:?}", scores);

    let team_name = String::from("Blue");
    // 插入，当前键不存在时，插入该键值对
    let score = scores.entry(team_name).or_insert(50);
    let score = scores.entry(String::from("Yellow")).or_insert(50);
    *score += 50;
    println!("{:?}", scores);

    let mut scores = HashMap::new();

    scores.insert(String::from("Blue"), 10);
    // 删除
    scores.remove(&String::from("Blue"));
    // 删除后，键值对不存在
    assert_eq!(scores.get(&String::from("Blue")), None);
}
